// 普通用户管理员模块下的store

// 引入storage
import Storage from '../../utils/storage'
import router from '../../router'
const STORAGE_INFO_KEY = 'user' // 存进去的键名

let storage = new Storage()

// 获取到存储中的用户信息更新到adminInfo中

let adminInfo = storage.getStorage(STORAGE_INFO_KEY)

// console.log(adminInfo)

// 从缓存获取到的用户信息不为空就就是者，否则用我们定义好的对象
adminInfo = adminInfo?adminInfo:{//账号信息
  access_token: "",
  account: "",
  add_ip: "",
  add_time: "",
  avatar: "",
  last_ip: "",
  last_time: "",
  nickname: "",
  phone: "",
  role: "",
  status: null,
  uid: null,
  user_role: null,
  token_gen: null,
  token_exp: null
}

// 如果有access_token就是已登录，没有就是未登录
let isLogin = adminInfo.access_token == ''?false:true

// 根据存储信息得到登录状态

const state= {
  namenick: '超级管理员',
  // 左边导航展开还是不展开参数
  isCollapse: false,
  // 登录状态
  isLogin,
  // 用户信息
  adminInfo,
  // 是否在进入登录页面的boolean值
  isEnterLoginPage:true,
  // 后台账号列表信息
  backstageTableList:null
}

const getters= {
  // 展开类
  getCollapse (state) {
    return state.isCollapse
  },

  //登陆状态获取
  getLogin (state) {
    return state.isLogin
  },

  getEnterLoginPage (state) {
    return state.isEnterLoginPage
  },

  // 获取登录得到的用户信息
  getUserInfo (state) {
    return state.adminInfo
  },

  // 获取到后台账号列表信息
  getBackstageTableList (state) {
    return state.backstageTableList
  }
}
const mutations= {
  isCollapseToggle (state) {
    // 将值传给左边导航
    state.isCollapse = !state.isCollapse
  },
  // // 登出修改isLogin
  // amendLogin (state) {
  //   state.isLogin = false
  //   console.log(state)
  // },

  // 存储的方法
  saveUserInfoSync (state, saveThings) {
    state.adminInfo = saveThings
  },
  logout (state) {
    state.adminInfo = {
      access_token: "",
      account: "",
      add_ip: "",
      add_time: "",
      avatar: "",
      last_ip: "",
      last_time: "",
      nickname: "",
      phone: "",
      role: "",
      status: null,
      uid: null,
      user_role: null
    }
  },
  // 进入登录页面操作
  enterLoginPage (state) {
    state.isEnterLoginPage = false
    state.isLogin = false
  },
  leaveLoginPage (state) {
    state.isEnterLoginPage = true
    state.isLogin = true
  },
  // 更改后台账号列表信息的方法
  changeBackstageTableList (state, changeThings) {
    state.backstageTableList = changeThings
    // console.log(changeThings)
  }
}
const actions= {
  // 异步提交给mutations
  saveUserInfo (context, saveThings) {
    return new Promise ((resolve, reject) => {
      // 键值为STORAGE_INFO_KEY，键名为saveThings(在工具函数中封装好了)
      storage.saveStorage(STORAGE_INFO_KEY, saveThings)
      // 提交给mutation
      context.commit('saveUserInfoSync', saveThings)
      resolve()
    })
  },
  removeUserInfo (context) {
    return new Promise((resolve,reject) => {
      storage.removeStorage(STORAGE_INFO_KEY)
      // 提交给mutation
      context.commit('logout')
      resolve()
    })
  }
}

export default{
  namespaced: true,// 开启模块的命名空间
  state,
  getters,
  mutations,
  actions
}